package cn.csg.ams.collect2.poptopics.task;


import cn.csg.ams.collect2.poptopics.dao.MkPrPfOutageLinkDataStatDao;
import cn.csg.ams.collect2.poptopics.entity.bo.MkPrPfOutageLinkDataStatBO;
import cn.csg.ams.collect2.poptopics.redisPool.LinkCountPool;
import cn.csg.ams.collect2.poptopics.infrastructure.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
public class DataCountTask {

    private static Logger logger = LoggerFactory.getLogger(DataCountTask.class);

    /**
     * 链路计数池
     */
    @Autowired
    private LinkCountPool linkCountPool;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Autowired
    private MkPrPfOutageLinkDataStatDao linkDataStatDao;


    /**
     * 每天凌晨1点，统计前一天的数据
     */
    // @Scheduled(cron = "0 0 1 * * ?")
    public void test() {
        System.out.println("定时任务执行。。。。。");

        // 获取前一天的数据
        Date yesterday = DateUtil.offsetDay(new Date(), -1);

        // 告警总数量
        Map<String, Long> alarmTotalNumMap = linkCountPool.getNum(LinkCountPool.ALARM_TOTAL_NUM_KEY, yesterday);
        // 告警及时数量
        Map<String, Long> alarmTimelyNumMap = linkCountPool.getNum(LinkCountPool.ALARM_TIMELY_NUM_KEY, yesterday);
        // 告警清洗数量
        Map<String, Long> alarmCleanNumMap = linkCountPool.getNum(LinkCountPool.ALARM_CLEAN_NUM_KEY, yesterday);
        // 负荷总数量
        Map<String, Long> loadTotalNumMap = linkCountPool.getNum(LinkCountPool.LOAD_TOTAL_NUM_KEY, yesterday);
        // 负荷及时数量
        Map<String, Long> loadTimelyNumMap = linkCountPool.getNum(LinkCountPool.LOAD_TIMELY_NUM_KEY, yesterday);
        // 负荷清洗数量
        Map<String, Long> loadCleanNumMap = linkCountPool.getNum(LinkCountPool.LOAD_CLEAN_NUM_KEY, yesterday);

        // 查询所有供电所编码
        jdbcTemplate.execute("set hg_experimental_enable_shard_pruning=OFF");
        // List<Map<String, Object>> supplyNoList = jdbcTemplate.queryForList("select mp_town_org_no from mk_pr_pf_outage_elink_query where mp_town_org_no is not null group by mp_town_org_no")
        List<Map<String, Object>> supplyNoList = jdbcTemplate.queryForList("select substring(area_code, 1, 4) as mp_town_org_no from mk_cu_elec_cust_mr_comp_view group by area_code");

        // 统计日期
        String statDate = DateUtil.format(yesterday, "yyyy-MM-dd");

        List<MkPrPfOutageLinkDataStatBO> list = new ArrayList<>();
        for (Map<String, Object> map : supplyNoList) {
            // 供电所编码
            String supplyNo = map.get("mp_town_org_no").toString();
            // 数量
            Long alarmTotalNum = alarmTotalNumMap.getOrDefault(supplyNo, 0L);
            Long alarmTimelyNum = alarmTimelyNumMap.getOrDefault(supplyNo, 0L);
            Long alarmCleanNum = alarmCleanNumMap.getOrDefault(supplyNo, 0L);
            Long loadTotalNum = loadTotalNumMap.getOrDefault(supplyNo, 0L);
            Long loadTimelyNum = loadTimelyNumMap.getOrDefault(supplyNo, 0L);
            Long loadCleanNum = loadCleanNumMap.getOrDefault(supplyNo, 0L);

            MkPrPfOutageLinkDataStatBO bo = new MkPrPfOutageLinkDataStatBO();
            bo.setStatDate(statDate);
            bo.setSupplyOrgNo(supplyNo);
            bo.setAlarmTotalNum(alarmTotalNum);
            bo.setAlarmTimelyNum(alarmTimelyNum);
            bo.setAlarmCleanNum(alarmCleanNum);
            bo.setLoadTotalNum(loadTotalNum);
            bo.setLoadTimelyNum(loadTimelyNum);
            bo.setLoadCleanNum(loadCleanNum);
            list.add(bo);
        }

        // 插入统计数据
        try {
            linkDataStatDao.insertBatch(list);
        } catch (Exception e) {
            logger.error("插入统计数据失败");
        }

        // 删除没有电流电压数据，且在停电状态, 且停电开始时间小于昨天的数据
        // jdbcTemplate.update(
        //         "delete from mk_pr_pf_outage_recovery_mon t1\n" +
        //                 "where \n" +
        //                 "outage_status = '1'\n" +
        //                 "and outage_start_time < ?\n" +
        //                 "and not exists (select 1 from mk_run_meter_cur_curve t2 where t1.run_meter_id = t2.run_meter_id)",
        //         new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yesterday)
        // );
    }

}
